package com.cqc.viewanimationutils01;

import android.animation.Animator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn)
    Button btn;
    @BindView(R.id.tv)
    TextView tv;
    private int cx;
    private int cy;
    private int radius;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        cx = (tv.getLeft() + tv.getRight()) / 2;
        cy = (tv.getTop()+tv.getBottom())/2;

    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        if (tv.getVisibility() == View.VISIBLE) {
//            radius = tv.getWidth();
            radius = (int) Math.hypot(tv.getWidth(),tv.getHeight());
            Animator animator = ViewAnimationUtils.createCircularReveal(tv, cx, cy, radius, 0);
            animator.setDuration(6000);
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    tv.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animator.start();
        } else {
            radius = Math.max(tv.getWidth(), tv.getHeight());
            Animator animator = ViewAnimationUtils.createCircularReveal(tv, cx, cy, 0, radius);
            animator.setDuration(6000);
            tv.setVisibility(View.VISIBLE);
            animator.start();
        }
    }
}
